<?php

namespace App\V3\V3_1\Models;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SerialNumberModel extends Model
{
    use HasFactory;

    protected $table = 'serial_number';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    protected $fillable = ['storage_manage_id','batch_no','serial_no','goods_id','status'];

    const USEDSTATUS = 0; //已使用
    const UNUSEDSTATUS = 1; //未使用

    //toArray 时间转换问题
    public static function find($serialId)
    {
        return self::where([
            'id' => $serialId,
            'status' => 1
        ])->first();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //入库
    public static function inSerialNumber($main_id,$serial_no,$num,$good_id,$storage_manage_d,$batch_no)
    {
            DB::beginTransaction();
            try {
                //开启后处理
                $serial_no_array = explode(',',$serial_no);

                if(!empty($serial_no_array)) {
                    if(empty($serial_no_array)){
                        throw new \Exception('序列号不能为空');
                    }

                    if(count($serial_no_array) != $num){
                        throw new \Exception('序列号个数与入库数量不符');
                    }

                    foreach ($serial_no_array as $v) {
                        //检查序列号是否使用
                        $number_info = SerialNumberModel::
                        select('materiel_name','status')->from('serial_number as a')
                        ->leftJoin('materiel_type as b','b.id','=','a.goods_id')
                        ->where(['goods_id' => $good_id,'serial_no' => $v,'b.main_id'=>$main_id])->first();
                        if(!empty($number_info)){
                            if($number_info['status'] == 1){
                                throw new \Exception('该商品'.$number_info['materiel_name'].'的序列号：'.$v.'已被占用');
                            }else{
                                throw new \Exception('该商品'.$number_info['materiel_name'].'的序列号：'.$v.'已被使用');                              
                            }
                        }
                        SerialNumberModel::insert([
                            'serial_no' => $v,
                            'batch_no' => $batch_no,
                            'storage_manage_id' => $storage_manage_d,
                            'goods_id' => $good_id,
                            'created_at' =>  time(),
                            'main_id'=>$main_id
                        ]);
                    }
                }

                DB::commit();
                return ['code' => 0, 'msg' => 'success', 'data' => ''];
            } catch(\Exception $e) {
                DB::rollBack();
                Log::debug('入库'.var_export($e->getMessage(), true));
                return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
            }

    }

    //出库
    public static function outSerialNumber($main_id,$serial_no,$num,$good_id)
    {
            DB::beginTransaction();
            try {
                //开启后处理
                $serial_no_array = explode(',',$serial_no);

                if(empty($serial_no_array)){
                    throw new \Exception('序列号不能为空');
                }

                if(count($serial_no_array) != $num){
                    throw new \Exception('序列号个数与入库数量不符');
                }

                foreach ($serial_no_array as $v) {  #dd($v);
                    $info = SerialNumberModel::where(['serial_no' => $v,'status' => self::UNUSEDSTATUS,
                        'goods_id' => $good_id,
                    ])->first();
                    #dd($v,$good_id);
                    if(empty($info)){
                        throw new \Exception('序列号:'.$v.'，不存在');
                    }
                    SerialNumberModel::where(['serial_no' => $v,'status' => self::UNUSEDSTATUS,
                        'goods_id' => $good_id,
                    ])->update(['status' => self::USEDSTATUS]);

                }

                DB::commit();
                return ['code' => 0, 'msg' => 'success', 'data' => ''];
            } catch(\Exception $e) {
                DB::rollBack();
                Log::debug('入库'.var_export($e->getMessage(), true));
                return ['code' => 1, 'msg' => $e->getMessage(), 'data' => ''];
            }

    }

    public static function getUserAddendum($main_id)
    {
        //判断是否开启
        $adminInfo = AdminAddendumModel::where(['main_id' =>$main_id])->first();

        $out_storage_way = $adminInfo['is_use_serial'];

        return $out_storage_way;
    }
}
